<?php
	class  models_cthoadon extends connect{
		function _listCTHoaDon(){
			$sql = "SELECT MaCT, MaMH, MaHD, SoLuongBan, DonGiaBan, ThanhTienBan
					FROM chitiethoadon";
			$this->query_db($sql);
			return $this->fetchAll();
		}
		function _delCTHoaDon($id){
			$sql = "DELETE 
					FROM chitiethoadon
					WHERE MaCT = '$id'";
			$this->query_db($sql);
		}
		function _addCTHoaHon($MaMH, $MaHD, $SoLuongBan, $DonGiaBan){
			$sql = "INSERT INTO chitiethoadon VALUES('','$MaMH','$MaHD','$SoLuongBan','$DonGiaBan','')";
			$this->query_db($sql);
		}
		function _getCTHoaDon($id){
			$sql = "SELECT MaCT, MaMH, MaHD, SoLuongBan, DonGiaBan, ThanhTienBan
					FROM chitiethoadon
					WHERE MaCT = '$id'";
			$this->query_db($sql);
			return $this->fetchOne();
		}
		function getCTHoaDon($id){
			$sql = "SELECT MaCT, MaMH, MaHD, SoLuongBan, DonGiaBan, ThanhTienBan
					FROM chitiethoadon
					WHERE MaHD = '$id'";
			$this->query_db($sql);
			return $this->fetchAll();
		}
		function _editCTHoaDon($MaMH, $SoLuongBan, $DonGiaBan, $id){
			$sql = "UPDATE chitiethoadon
					SET
						MaMH = '$MaMH',
						SoLuongBan = '$SoLuongBan',
						DonGiaBan = '$DonGiaBan'
					WHERE MaCT = '$id'";
			$this->query_db($sql);
		}
		function sanphanbanchay(){
			$sql="SELECT DISTINCT (MaMH),SoLuongBan
					FROM chitiethoadon
					WHERE SoLuongBan >(select AVG(SoLuongBan) from chitiethoadon)
					OR SoLuongBan =(select AVG(SoLuongBan) from chitiethoadon)";
			$this->query_db($sql);
			return $this->fetchAll();
		}
	}
?>